package com.ihemou.common.security.aspect;


import com.ihemou.common.core.constant.Constants;
import com.ihemou.common.core.constant.HttpStatus;
import com.ihemou.common.core.exception.ServiceException;
import com.ihemou.common.core.utils.ServletUtils;
import com.ihemou.common.core.utils.StringUtils;
import com.ihemou.common.security.annotation.ServiceRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * @author saperliu
 * @version 1.0.0
 * @date 2021-10-01 21:48
 * @Description 只开放给内部服务之间访问，对外不开放
 */
@Aspect
@Component
public class ServiceRequestAspect implements Ordered
{
    @Around("@annotation(serviceRequest)")
    public Object innerAround(ProceedingJoinPoint point, ServiceRequest serviceRequest) throws Throwable
    {
        String request = ServletUtils.getRequest().getHeader(Constants.SERVICE_REQUEST);
        // 内部请求验证
        if (!StringUtils.equals(Constants.REQUEST_INNER, request))
        {
            throw new ServiceException("内部服务，不允许访问", HttpStatus.FORBIDDEN);
        }
        return point.proceed();
    }

    /**
     * 确保在权限认证aop执行前执行
     */
    @Override
    public int getOrder()
    {
        return Ordered.HIGHEST_PRECEDENCE + 1;
    }
}
